<?php
/**
 * Created by PhpStorm.
 * User: caipeichao
 * Date: 14-3-10
 * Time: PM9:01
 */

namespace Weibo\Model;

use Think\Model;

class ExpressionModel extends Model
{
    public $pkg = 'all';

    /**
     * 获取当前主题包下所有的表情
     * @param boolean $flush 是否更新缓存，默认为false
     * @return array 返回表情数据
     */
    public function getAllExpression()
    {
        define('ROOT_PATH', str_replace('/Application/Weibo/Model/ExpressionModel.class.php', '', str_replace('\\', '/', __FILE__)));
        $pkg = $this->pkg; //TODO 临时写死
        if($pkg == 'all'){
            return $this->getAll();
        }
        else{
            $filepath = ROOT_PATH . "/Public/static/image/expression/" . $pkg;
            $list = $this->myreaddir($filepath);
            $res = array();
            foreach ($list as $value) {
                $file = explode(".", $value);
                $temp['title'] = $file[0];
                $temp['emotion'] = '[' . $file[0] . ':' . $pkg . ']';
                $temp['filename'] = $value;
                $temp['type'] = $pkg;
                $temp['src'] = __ROOT__ . "/Public/static/image/expression/" . $pkg . '/' . $value;
                $res[$temp['emotion']] = $temp;
            }
            return $res;
        }

    }

    /**
     * getAll 获取所有主题的所有表情
     * @return array
     * @author:xjw129xjt xjt@ourstu.com
     */
    public function getAll()
    {
        define('ROOT_PATH', str_replace('/Application/Weibo/Model/ExpressionModel.class.php', '', str_replace('\\', '/', __FILE__)));
        $ExpressionPkg = ROOT_PATH . "/Public/static/image/expression";
        $pkgList = $this->myreaddir($ExpressionPkg);
        $res = array();
        foreach ($pkgList as $v) {
            $filepath = ROOT_PATH . "/Public/static/image/expression/" . $v;
            $list = $this->myreaddir($filepath);
            foreach ($list as $value) {
                $file = explode(".", $value);
                $temp['title'] = $file[0];
                $temp['emotion'] = '[' . $file[0] . ':' . $v . ']';
                $temp['filename'] = $value;
                $temp['type'] = $v;
                $temp['src'] = __ROOT__ . "/Public/static/image/expression/" . $v . '/' . $value;
                $res[$temp['emotion']] = $temp;
            }
        }
        return $res;
    }

    public function myreaddir($dir)
    {
        $file = scandir($dir, 0);
        $i = 0;
        foreach ($file as $v) {
            if (($v != ".") and ($v != "..")) {
                $list[$i] = $v;
                $i = $i + 1;
            }
        }
        /*        $handle = opendir($dir);
                $i = 0;
                while ($file = readdir($handle)) {
                    if (($file != ".") and ($file != "..")) {
                        $list[$i] = $file;
                        $i = $i + 1;
                    }
                }
                closedir($handle);*/

        return $list;
    }


    public function getSmileSrc($filename)
    {
        $ExpressionPkg = ROOT_PATH . "/Public/static/image/expression";
        $pkgList = $this->myreaddir($ExpressionPkg);
        foreach ($pkgList as $v) {
            if (file_exists($ExpressionPkg . '/' . $v . '/' . $filename)) {
                return __ROOT__ . "/Public/static/image/expression" . "/" . $v . "/" . $filename;
            }
        }
        return false;
    }

    /**
     * 将表情格式化成HTML形式
     * @param string $data 内容数据
     * @return string 转换为表情链接的内容
     */
    public function parse($data)
    {
        $data = preg_replace("/img{data=([^}]*)}/", "<img src='$1'  data='$1' >", $data);
        return $data;
    }
}















